namespace SubSonic.Sugar
{
    using System;

    public static class Dates
    {
        public static int CountWeekdays(DateTime startTime, DateTime endTime)
        {
            TimeSpan span = (TimeSpan) (endTime - startTime);
            Console.WriteLine(span.Days);
            int num = 0;
            for (int i = 0; i < span.Days; i++)
            {
                if (IsWeekDay(startTime.AddDays((double) i)))
                {
                    num++;
                }
            }
            return num;
        }

        public static int CountWeekends(DateTime startTime, DateTime endTime)
        {
            TimeSpan span = (TimeSpan) (endTime - startTime);
            Console.WriteLine(span.Days);
            int num = 0;
            for (int i = 0; i < span.Days; i++)
            {
                if (IsWeekEnd(startTime.AddDays((double) i)))
                {
                    num++;
                }
            }
            return num;
        }

        public static DateTime DaysAgo(int days)
        {
            TimeSpan span = new TimeSpan(days, 0, 0, 0);
            return DateTime.Now.Subtract(span);
        }

        public static DateTime DaysFromNow(int days)
        {
            TimeSpan span = new TimeSpan(days, 0, 0, 0);
            return DateTime.Now.Add(span);
        }

        public static TimeSpan Diff(DateTime dateOne, DateTime dateTwo)
        {
            return dateOne.Subtract(dateTwo);
        }

        public static double DiffDays(DateTime dateOne, DateTime dateTwo)
        {
            return Diff(dateOne, dateTwo).TotalDays;
        }

        public static double DiffDays(string dateOne, string dateTwo)
        {
            DateTime time;
            DateTime time2;
            if (DateTime.TryParse(dateOne, out time) && DateTime.TryParse(dateTwo, out time2))
            {
                return Diff(time, time2).TotalDays;
            }
            return 0.0;
        }

        public static double DiffHours(DateTime dateOne, DateTime dateTwo)
        {
            return Diff(dateOne, dateTwo).TotalHours;
        }

        public static double DiffHours(string dateOne, string dateTwo)
        {
            DateTime time;
            DateTime time2;
            if (DateTime.TryParse(dateOne, out time) && DateTime.TryParse(dateTwo, out time2))
            {
                return Diff(time, time2).TotalHours;
            }
            return 0.0;
        }

        public static double DiffMinutes(DateTime dateOne, DateTime dateTwo)
        {
            return Diff(dateOne, dateTwo).TotalHours;
        }

        public static double DiffMinutes(string dateOne, string dateTwo)
        {
            DateTime time;
            DateTime time2;
            if (DateTime.TryParse(dateOne, out time) && DateTime.TryParse(dateTwo, out time2))
            {
                return Diff(time, time2).TotalMinutes;
            }
            return 0.0;
        }

        private static string FormatString(string str, string previousStr, int t)
        {
            if ((t == 0) && (previousStr.Length == 0))
            {
                return string.Empty;
            }
            if (t == 1)
            {
                str = t + " " + str;
            }
            else
            {
                str = string.Concat(new object[] { t, " ", str, "s" });
            }
            return (str + " ");
        }

        public static string GetDateDayWithSuffix(DateTime date)
        {
            int day = date.Day;
            switch (day)
            {
                case 1:
                case 0x15:
                case 0x1f:
                    return (day + "st");

                case 2:
                case 0x16:
                    return (day + "nd");
            }
            if ((day != 3) && (day != 0x17))
            {
                return (day + "th");
            }
            return (day + "rd");
        }

        public static string GetFormattedMonthAndDay(DateTime date)
        {
            return (string.Format("{0:MMMM}", date) + " " + GetDateDayWithSuffix(date));
        }

        public static DateTime HoursAgo(int hours)
        {
            TimeSpan span = new TimeSpan(hours, 0, 0);
            return DateTime.Now.Subtract(span);
        }

        public static DateTime HoursFromNow(int hours)
        {
            TimeSpan span = new TimeSpan(hours, 0, 0);
            return DateTime.Now.Add(span);
        }

        public static bool IsDate(object dt)
        {
            DateTime time;
            return DateTime.TryParse(dt.ToString(), out time);
        }

        public static bool IsWeekDay(DateTime dt)
        {
            return (Convert.ToInt16(dt.DayOfWeek) < 6);
        }

        public static bool IsWeekEnd(DateTime dt)
        {
            return (Convert.ToInt16(dt.DayOfWeek) > 5);
        }

        public static DateTime MinutesAgo(int minutes)
        {
            TimeSpan span = new TimeSpan(0, minutes, 0);
            return DateTime.Now.Subtract(span);
        }

        public static DateTime MinutesFromNow(int minutes)
        {
            TimeSpan span = new TimeSpan(0, minutes, 0);
            return DateTime.Now.Add(span);
        }

        public static string ReadableDiff(DateTime startTime, DateTime endTime)
        {
            string str = "";
            int num = endTime.Second - startTime.Second;
            int num2 = endTime.Minute - startTime.Minute;
            int num3 = endTime.Hour - startTime.Hour;
            int num4 = endTime.Day - startTime.Day;
            int num5 = endTime.Month - startTime.Month;
            int num6 = endTime.Year - startTime.Year;
            if (num < 0)
            {
                num2--;
                num += 60;
            }
            if (num2 < 0)
            {
                num3--;
                num2 += 60;
            }
            if (num3 < 0)
            {
                num4--;
                num3 += 0x18;
            }
            if (num4 < 0)
            {
                num5--;
                int month = (endTime.Month == 1) ? 12 : (endTime.Month - 1);
                int year = (month == 12) ? (endTime.Year - 1) : endTime.Year;
                num4 += DateTime.DaysInMonth(year, month);
            }
            if (num5 < 0)
            {
                num6--;
                num5 += 12;
            }
            if (num6 > 0)
            {
                return (num6.ToString() + " years, " + num5.ToString() + " months ago");
            }
            if (num5 > 0)
            {
                return (num5.ToString() + " months, " + num4.ToString() + " days ago");
            }
            if (num4 > 0)
            {
                return (num4.ToString() + " days, " + num3.ToString() + " hours ago");
            }
            if (num3 > 0)
            {
                return (num3.ToString() + " hours, " + num2.ToString() + " minutes ago");
            }
            if (num2 > 0)
            {
                str = num2.ToString() + " minutes ago";
            }
            return str;
        }

        public static DateTime SecondsAgo(int seconds)
        {
            TimeSpan span = new TimeSpan(0, 0, seconds);
            return DateTime.Now.Subtract(span);
        }

        public static DateTime SecondsFromNow(int seconds)
        {
            TimeSpan span = new TimeSpan(0, 0, seconds);
            return DateTime.Now.Add(span);
        }

        public static string TimeDiff(DateTime startTime, DateTime endTime)
        {
            int t = endTime.Second - startTime.Second;
            int num2 = endTime.Minute - startTime.Minute;
            int num3 = endTime.Hour - startTime.Hour;
            int num4 = endTime.Day - startTime.Day;
            int num5 = endTime.Month - startTime.Month;
            int num6 = endTime.Year - startTime.Year;
            if (t < 0)
            {
                num2--;
                t += 60;
            }
            if (num2 < 0)
            {
                num3--;
                num2 += 60;
            }
            if (num3 < 0)
            {
                num4--;
                num3 += 0x18;
            }
            if (num4 < 0)
            {
                num5--;
                int month = (endTime.Month == 1) ? 12 : (endTime.Month - 1);
                int year = (month == 12) ? (endTime.Year - 1) : endTime.Year;
                num4 += DateTime.DaysInMonth(year, month);
            }
            if (num5 < 0)
            {
                num6--;
                num5 += 12;
            }
            string previousStr = FormatString("year", string.Empty, num6);
            string str2 = FormatString("month", previousStr, num5);
            string str3 = FormatString("day", str2, num4);
            string str4 = FormatString("hour", str3, num3);
            string str5 = FormatString("minute", str4, num2);
            string str6 = FormatString("second", str5, t);
            return (previousStr + str2 + str3 + str4 + str5 + str6);
        }
    }
}

